//
//  LeftOfSelect.m
//  HYHoney
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LeftOfSelect.h"
#import "RESideMenu.h"
#import "CellOfSelect.h"
#import "ModelOfImg.h"
#import "UIImageView+WebCache.h"

#import "CellForSN.h"
#import "CellOfShouN.h"
#import "CellForChuF.h"
#import "CellForZW.h"
#import "CellForHuaY.h"

#import "PushOfSelect.h"
#import "DKNightVersion.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

#define WIDTH2 [UIScreen mainScreen].bounds.size.width
#define HEIGHT2 [UIScreen mainScreen].bounds.size.height

@interface LeftOfSelect ()<UICollectionViewDelegate,UICollectionViewDataSource,NSURLSessionDataDelegate,NSURLSessionDelegate,NSURLSessionTaskDelegate,CellForSNDelegate,CellForShouNDelegate,CellForChuFDelegate,CellForZWDelegate,CellForHuaYDelegate>

@property (nonatomic,retain)NSMutableArray *arrayTitle;
@property (nonatomic,retain)UIScrollView *scrollView;
@property (nonatomic,retain)UIView *viewForline;
@property (nonatomic,retain)UICollectionView *collectView;
@property (nonatomic,retain)UICollectionViewFlowLayout *flowLayout;

@end

@implementation LeftOfSelect

- (void)dealloc{

    [_arrayTitle release];
    [_scrollView release];
    [_viewForline release];
    [_collectView release];
    [_flowLayout release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationItem.title = @"家居推荐";
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:1 green:1 blue:1 alpha:1], NSForegroundColorAttributeName, nil]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xial"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    

    [self createCollectionView];
    [self createKVOAction];
    [self createScrollView];
}


- (void)createScrollView{

    self.arrayTitle = @[@"室内", @"收纳", @"厨房", @"杂物", @"花园"].mutableCopy;
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 40)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = YES;
    
    self.scrollView.dk_backgroundColorPicker = DKColorWithRGB(0xBFEFFF, 0xB0E2FF);
    
    self.scrollView.tintColor = [UIColor blackColor];
    
    self.view.dk_backgroundColorPicker = DKColorWithRGB(0xFFFAFA, 0x27408B);
    
    [self.view addSubview:self.scrollView];
    
    self.scrollView.contentSize = CGSizeMake(WIDTH / 5 * self.arrayTitle.count, 0);
    
    
    [self.scrollView release];
    
    for (int i = 0; i < self.arrayTitle.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:self.arrayTitle[i] forState:UIControlStateNormal];
        
        button.frame = CGRectMake(WIDTH / 5 *  i , 10, WIDTH / 5 - 10, 30);
        button.tag = i + 1;
        
        [button addTarget:self action:@selector(handleOfbutton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.scrollView addSubview:button];
    }
    
    [self createLine];
}


- (void)createLine{
    
    self.viewForline = [[UIView alloc]initWithFrame:CGRectMake(0, 100, WIDTH/ 5 - 10, 2)];
    
    self.viewForline.backgroundColor = [UIColor blackColor];
    self.viewForline.alpha = 0.4f;
    [self.view addSubview:self.viewForline];
    
    [self.viewForline release];
}

- (void)handleOfbutton:(UIButton *)button{
    
    NSIndexPath *path = [NSIndexPath indexPathForItem:button.tag - 1 inSection:0];
    
    [self.collectView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}


- (void)createCollectionView{
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT2 - 113);
    
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    
    self.flowLayout.scrollDirection =UICollectionViewScrollDirectionHorizontal;
    
    //collection
    self.collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30, WIDTH, HEIGHT2 - 30) collectionViewLayout:self.flowLayout];
    
    [self.view addSubview:self.collectView];
    self.collectView.dataSource = self;
    self.collectView.delegate = self;
    self.collectView.pagingEnabled = YES;
    self.collectView.dk_backgroundColorPicker = DKColorWithRGB(0xFFFAFA, 0x27408B);
    self.collectView.showsVerticalScrollIndicator = NO;
    self.collectView.showsHorizontalScrollIndicator = NO;
    
    [self.collectView registerClass:[CellForSN class] forCellWithReuseIdentifier:@"poolForCell"];
    [self.collectView registerClass:[CellOfShouN class] forCellWithReuseIdentifier:@"poolForCell1"];
    [self.collectView registerClass:[CellForChuF class] forCellWithReuseIdentifier:@"poolForCell2"];
    [self.collectView registerClass:[CellForZW class] forCellWithReuseIdentifier:@"poolForCell3"];
    [self.collectView registerClass:[CellForHuaY class] forCellWithReuseIdentifier:@"poolForCell4"];
    
    [self.flowLayout release];
    [self.collectView release];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arrayTitle.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.item == 0) {
    
        CellForSN *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell" forIndexPath:indexPath];
  
        cell.delegate = self;
        cell.backgroundColor = [UIColor yellowColor];
        return cell;
        
    }else if (indexPath.item == 1){

        CellOfShouN *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell1" forIndexPath:indexPath];
        
        cell.delegate = self;
        
        return cell;
        
    }else if (indexPath.item == 2){
        
        CellForChuF *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell2" forIndexPath:indexPath];
        
        cell.delegate = self;
        
        return cell;
        
    }else if (indexPath.item == 3){
        
        CellForZW *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell3" forIndexPath:indexPath];
        
        cell.delegate = self;
        
        return cell;
        
    }else {
        
        CellForHuaY *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell4" forIndexPath:indexPath];
        
        cell.delegate = self;
        
        return cell;
    }

}


#pragma mark - CellForpicUrlCollectionViewCell的协议方法
- (void)didClickSelect:(ModelOfImg *)model {
    
    PushOfSelect *show = [[PushOfSelect alloc]init];
    
//    ModelOfImg *model = [[ModelOfImg alloc] init];
    
    show.url = model.url;
    
//    NSLog(@"%@",show.url);
    
    [self.navigationController pushViewController:show animated:NO];
}


#pragma mark - 知识点1 KVO(键值观察者)
/** 核心: 观察者 观察 某一个对象的属性变化 */
- (void)createKVOAction{
    
    //VC 观察 collection 的 collectionOffset 属性
    [self.collectView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"context"];
}


/** 当被观察者属性发生变化的时候,调用此方法 */   // KVO
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context{
    
    CGFloat x = [[change objectForKey:@"new"]CGPointValue].x;
    // CGFloat redX = x * WIDTH /5  /WIDTH;
    CGFloat redX = x / 5  ;
    
    // 修改 RedLine 位置
    self.viewForline.transform = CGAffineTransformMakeTranslation(redX, 0);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
